﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KD.Sms.UI_Sys
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Query":
                    DoBind();
                    break;
                case "Add":
                    frmUserEdit obj = new frmUserEdit();
                    obj.ShowDialog();
                    DoBind();
                    break;
            }
        }

        /// <summary>
        /// 绑定数据显示
        /// </summary>
        private void DoBind()
        {
            gridMain.DataSource = DAL.SqlLite.Main.Instance.DoQueryUserList(txtUser.Text.Trim());
            foreach (DataGridViewRow gr in gridMain.Rows)
            {
                if (gr.Index >= 0)
                {
                    if (gr.Cells["User_State"].Value.ToString() == "禁用")
                    {
                        gr.Cells["User_State"].Style.ForeColor = Color.Red;
                    }
                    else
                    {
                        gr.Cells["User_State"].Style.ForeColor = Color.Green;
                    }

                    //状态为隐藏
                    if (gr.Cells["User_KeyInformation"].Value.ToString() == "隐藏")
                    {
                        gr.Cells["User_KeyInformation"].Style.ForeColor = Color.Red;
                    }
                    else
                    {
                        gr.Cells["User_KeyInformation"].Style.ForeColor = Color.Green;
                    }
                }
            }
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            DoBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridMain_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (gridMain.Columns[e.ColumnIndex].Name == "XG")
                {
                    frmUserEdit obj = new frmUserEdit(gridMain.Rows[e.RowIndex]);
                    obj.ShowDialog();
                    DoBind();
                }
                else if (gridMain.Columns[e.ColumnIndex].Name == "CZ")
                {
                    var userid = gridMain.Rows[e.RowIndex].Cells["User_Id"].Value.ToString();
                    if (gridMain.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "启用")
                    {
                        DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功启用用户[{0}]", gridMain.Rows[e.RowIndex].Cells["Chinese_name"].Value.ToString()));
                        DAL.SqlLite.Main.Instance.DoUpdateUserState(userid, true);
                    }
                    else
                    {
                        DAL.SqlLite.Main.Instance.DoUpdateUserState(userid, false);
                        DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功禁用用户[{0}]", gridMain.Rows[e.RowIndex].Cells["Chinese_name"].Value.ToString()));
                    }
                    DoBind();
                }

                //显示或隐藏信息
                else if (gridMain.Columns[e.ColumnIndex].Name == "XS")
                {
                    var userid = gridMain.Rows[e.RowIndex].Cells["User_Id"].Value.ToString();
                    if (gridMain.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "显示")
                    {
                        DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功显示用户[{0}]关键信息", gridMain.Rows[e.RowIndex].Cells["Chinese_name"].Value.ToString()));
                        DAL.SqlLite.Main.Instance.DoUpdateUserKeyInformation(userid, true);
                    }
                    else
                    {
                        DAL.SqlLite.Main.Instance.DoUpdateUserKeyInformation(userid, false);
                        DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功隐藏用户[{0}]关键信息", gridMain.Rows[e.RowIndex].Cells["Chinese_name"].Value.ToString()));
                    }
                    DoBind();
                }
            }
        }
    }
}
